<template>
  <el-card style="margin: 10px;">
    <div class="search">
      <el-input
        placeholder="用户名"
        size="small"
        style="width:200px"
        v-model="userName"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="inquireusername"
        >查询</el-button
      >
      <el-button size="small" @click="emptyusername">清空</el-button>
    </div>
    <el-button type="primary" size="samll" @click="addOrupdateUser(null)"
      >添加</el-button
    >
    <el-button
      type="danger"
      size="samll"
      @click="AllCheckedRemove"
      :disabled="!AllChecked.length >= 1"
      >批量删除</el-button
    >
    <el-table
      border
      style="margin-top: 20px;"
      :data="pageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="用户名"
        width="180px"
        prop="username"
      ></el-table-column>
      <el-table-column
        label="用户昵称"
        prop="nickName"
        width="100px"
      ></el-table-column>
      <el-table-column
        label="角色列表"
        prop="roleName"
        width="180px"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="gmtCreate"
        width="220px"
      ></el-table-column>
      <el-table-column
        label="更新时间"
        prop="gmtModified"
        width="220px"
      ></el-table-column>
      <el-table-column label="操作" fixed="right" width="240px" align="center">
        <template v-slot="{ row }">
          <el-button
            size="mini"
            type="info"
            icon="el-icon-user-solid"
            title="分配角色"
            @click="ShowRole(row)"
          ></el-button>

          <el-button
            style="margin-right: 10px;"
            size="mini"
            type="primary"
            icon="el-icon-edit"
            title="修改用户"
            @click="addOrupdateUser(row)"
          ></el-button>
          <el-popconfirm
            title="这是一段内容确定删除吗？"
            @onConfirm="removeUser(row.id)"
          >
            <el-button
              size="mini"
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              title="删除用户"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 20, 30]"
      background
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      style="margin-top: 20px;"
    >
    </el-pagination>

    <!-- 添加或者删除 -->
    <el-dialog
      :title="`${userInfo.id ? '修改' : '添加'}用户`"
      :visible="dialogFormVisible"
      top="0px"
    >
      <el-form
        :model="userInfo"
        label-width="100px"
        :rules="rules"
        ref="userForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userInfo.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="userInfo.nickName"></el-input>
        </el-form-item>
        <el-form-item v-if="!userInfo.id" label="用户密码" prop="password">
          <el-input v-model="userInfo.password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="saveUser">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 设置角色 -->
    <UserRole
      :isShowRole.sync="isShowRole"
      :id.sync="id"
      :username="username"
      @getPageList="getPageList"
    ></UserRole>
  </el-card>
</template>

<script>
import {
  getUserPageListApi,
  updateUserApi,
  saveUserApi,
  removeUserApi,
  batchRemoveApi
} from "@/api/acl/user.js";

import { Message } from "element-ui";
import UserRole from "./components/userRole.vue";
export default {
  name: "User",
  components: {
    UserRole
  },
  data() {
    return {
      // 分页列表
      pageList: [],
      page: 1,
      limit: 5,
      total: 0,
      dialogFormVisible: false,
      userInfo: {
        id: null,
        // 用户昵称
        nickName: "",
        // 用户名
        username: "",
        // 密码
        password: ""
      },

      id: null,
      username: "",
      // 控制角色的显示与隐藏
      isShowRole: false,

      // 所有选中的id的数组
      AllChecked: [],
      // 输入框中的用户名
      userName: "",
      // 表单校验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 9, message: "长度在2 到 9 个字符", trigger: "change" }
        ],

        password: [
          { required: true, message: "请填写密码", trigger: "blur" },
          { min: 3, max: 18, message: "密码长度为3 到18 位", trigger: "change" }
        ]
      }
    };
  },
  mounted() {
    this.getPageList();
  },
  methods: {
    // 得到选中的事件
    handleSelectionChange(val) {
      this.AllChecked = val.map(item => item.id);
    },

    // 点击批量删除
    async AllCheckedRemove() {
      this.$confirm("删除选中的用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await batchRemoveApi(this.AllChecked);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getPageList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 点击查询
    inquireusername() {
      // console.log(this.userName);
      this.getPageList(this.userName);
    },
    // 点击清空
    emptyusername() {
      this.userName = "";
      this.getPageList();
    },
    // 获取分页列表
    async getPageList(UserName = "") {
      const result = await getUserPageListApi(this.page, this.limit, UserName);
      // console.log(result);
      this.pageList = result.data.items;
      this.total = result.data.total;
    },
    // 改变每页条数
    handleSizeChange(val) {
      this.limit = val;
      this.getPageList(this.userName);
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.page = val;
      this.getPageList(this.userName);
    },

    // 清除数据
    cleardate() {
      this.userInfo.id = null;
      this.userInfo.username = "";
      this.userInfo.nickName = "";
      this.userInfo.password = "";
    },
    // 取消
    cancel() {
      this.dialogFormVisible = false;
      this.cleardate();
      this.$refs.userForm.clearValidate([]);
    },
    // 添加或者修改
    async addOrupdateUser(row) {
      this.dialogFormVisible = true;
      if (row) {
        this.userInfo.id = row.id;
        this.userInfo.username = row.username;
        this.userInfo.nickName = row.nickName;
      } else {
        this.cleardate();
      }
    },

    // 提交
    saveUser() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          if (this.userInfo.id) {
            await updateUserApi(this.userInfo);
          } else {
            await saveUserApi(this.userInfo);
          }
          Message.success("保存成功");
          this.getPageList();
          this.dialogFormVisible = false;
          this.cleardate();
        }
      });
    },

    // 删除
    async removeUser(id) {
      await removeUserApi(id);
      Message.success("删除成功");
      this.getPageList();
    },

    async ShowRole(row) {
      this.isShowRole = true;
      this.id = row.id;
      this.username = row.username;
    }
  }
};
</script>

<style lang="scss" scoped>
.search {
  display: flex;
  margin-bottom: 20px;

  button {
    margin-left: 10px;
  }
}
</style>
